package com.browsesoft.adapter;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Hashtable;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLCalendarModel;
import com.browsesoft.user.User;

public class EveryDayDuty implements HTMLCalendarModel {
	private String userid = new String();

	public EveryDayDuty() {
	};

	/**
	 * 预约
	 * 
	 * @param gc
	 *            日历
	 * @return 是否有预约
	 */
	public boolean isAppoint(GregorianCalendar gc) {
		try {
			Calendar justTime = new GregorianCalendar();
			justTime.setTime(new Date());
			String month = Integer.toString(gc.get(GregorianCalendar.MONTH));
			String day = Integer.toString(gc
					.get(GregorianCalendar.DAY_OF_MONTH));
			if (month.length() == 1) {
				month = "0" + month;
			}
			if (day.length() == 1) {
				day = "0" + day;
			}
			String date = Integer.toString(gc.get(GregorianCalendar.YEAR))
					+ month + day + ";";
			Hashtable ht = new Hashtable();
			// 放入查询条件
			ht.put("parentid", date);
			ht.put("userid", userid);
			// 得到符合条件的实体
			LinkedList result = com.browsesoft.EntityManager.getInstance()
					.getObjects("com.browsesoft.adapter.CalendarFormModel", ht);

			if (result.size() >= 1) {
				return true;
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	/**
	 * 初始化model
	 * 
	 * @param request
	 *            客户端请求
	 * @throws Exception
	 */
	public void init(HttpServletRequest request) throws Exception {
	};

	/**
	 * 处理用户请求
	 * 
	 * @param request
	 *            客户端请求
	 * @param response
	 *            响应
	 * @throws Exception
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = (User) request.getSession().getAttribute("loginUser");
		userid = user.getID();
	}

	/**
	 * 设置模型所对应的组件
	 * 
	 * @param component
	 *            对应的组件
	 */
	public void setComponent(HTMLBasicComponent component) {
	};

	/**
	 * 由Component决定需要Model处理的元素内容，在Component的updateElement()方法中调用
	 * 
	 * @param c
	 *            基本组件
	 * @throws Exception
	 */
	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	};
}
